﻿using Payroll.BusinessLogic.Caching;
using Payroll.BusinessLogic.Data;
using Payroll.BusinessLogic.Messages;
using Payroll.BusinessLogic.Security;
using Payroll.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace Payroll.BusinessLogic.MyCompany
{
    public partial class CompanyService : ICompanyService
    {
        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly PayrollHRDataContext _context;
        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public CompanyService(PayrollHRDataContext context)
        {
            this._context = context;
            this._cacheManager = new PayrollRequestCache();
        }

        #endregion
        public EmailAccounts GetAccount(int companyid, int option)
        {
            var query = (from mc in _context.MyCompanies
                         where mc.MyCompanyID == companyid
                         select mc).FirstOrDefault();
            EmailAccounts emailaccount = new EmailAccounts
            {
                DisplayName = query.Name,
                Email = option == 0 ? query.Receive : (option == 1 ? query.EmailSender : query.EmailSend),
                EnableSSL = true,
                Host = option == 0 ? "pop.gmail.com" : "smtp.gmail.com",
                Port = option == 0 ? 995 : 587,
                Password = "chaiko9.0"
            };
            return null;
        }        
        public Company Save(Company company)
        {
            var item = (from mc in _context.MyCompanies
                        where mc.MyCompanyID == company.MyCompanyID
                        select mc).FirstOrDefault();
            if (!CommonHelper.IsNull(item))
            {
                item.Name = company.Name;
                item.Address = company.Address;
                item.EmailSend = company.EmailSend;
                item.TaxCode = company.TaxCode;
                item.Receive = company.Receive;
                item.Modified = company.Modified;
                item.ModifiedDate = company.ModifiedDate;
                item.IsActive = company.IsActive;
                item.EmailSender = company.EmailSender;
                item.Host = company.Host;
                item.Port = company.Port; 
                item.EmailPassword = SecurityHelper.Encrypt(company.EmailPassword);
            }
            else {
                item = new Payroll.BusinessLogic.Data.MyCompany
                {
                    Name = company.Name,
                    Address = company.Address,
                    EmailSend = company.EmailSend,
                    TaxCode = company.TaxCode,
                    Receive = company.Receive,
                    Createby = company.Createby,
                    Modified = company.Modified,
                    CreateDate = company.CreateDate,
                    ModifiedDate = company.ModifiedDate,
                    IsActive = company.IsActive,
                    EmailSender = company.EmailSender,
                    Host = company.Host,
                    Port = company.Port,
                    EmailPassword = SecurityHelper.Encrypt(company.EmailPassword),
                };
                _context.MyCompanies.InsertOnSubmit(item);
            }
            _context.SubmitChanges();
            return company;
        }
        public Company GetCompany()
        {
            var query = (from mc in _context.MyCompanies
                         select new Company {
                             MyCompanyID = mc.MyCompanyID,
                             Name = mc.Name,
                             Address = mc.Address,
                             EmailSend = mc.EmailSend,
                             TaxCode = mc.TaxCode,
                             Receive = mc.Receive,
                             Createby = mc.Createby,
                             Modified = mc.Modified,
                             CreateDate = mc.CreateDate,
                             ModifiedDate = mc.ModifiedDate,
                             IsActive = mc.IsActive.Value,
                             EmailSender = mc.EmailSender,
                             Host = mc.Host,
                             Port = mc.Port?? mc.Port.Value,
                             EmailPassword = SecurityHelper.Decrypt(mc.EmailPassword)
                         }).FirstOrDefault();
            return query; 
        }
    }
}
